
function numbersOnly(string) {
    if (string.search(/^[0-9\.]*$/) != -1)
         return true;
     else
         return false;
}




var swakRetail = null;
var swakGGPrice = null;
var swakStartTime = null;
var swakSliderRate = null;
var swakUpdateId = null;
var swakjellyfishstrings = null;

function t2pinputchange(event) {
	var key = event.keyCode;
	if (key == 13) {
		//enter key
		var result = ggsubmit();
		if (result) {
			window.close();
		}
	}
	var relans = content.document.getElementById('t2prelativeans');
//	var absans = content.document.getElementById('t2pabsans');
	var gginput = content.document.getElementById('t2pinput');
	var percentinput = content.document.getElementById('t2ppercentinput');
//	var ggans = content.document.getElementById('t2panswer');
	var ggcomment = content.document.getElementById('t2pcomment');
	var jellyfishstrings = window.opener.document.getElementById("swaksmack-strings");
//	alert(gginput.value + ' gginputchange ' + event.target.value);

	var svalue=gginput.value;
	if (svalue==null || svalue=='') {
	//	ggans.textContent='';
		ggcomment.textContent='';
		relans.value = "";
		percentinput.value = "";
	}
	else if (!numbersOnly(svalue)) {
		gginput.value=svalue.substring(0,svalue.length-1);
	  ggcomment.textContent=jellyfishstrings.getString('ggcalc_error2');
		//handle.popup.alert('numbers only');

	}
	else {
		//	alert('else... ' + window.opener.content.document);
		var maindoc = window.opener.content.document;
	
    			var product = content.document.getElementById('t2pproductprice');
    			//alert('retail calc tc ' + product.textContent);
    			//handle.popup.alert('gg guruinput FOUND' );
    			var result = t2pcalcPrice(swakRetail,event.target.value);
    			if (result == null) {
    				ggcomment.textContent='';
    				//alert('Guru Guess Calculator missing retail or user final price.  Sorry you are on your own...');
    				return;
    			}

    		//	ggans.textContent=result + '%';
    			
    			if(result <15) {
    				ggcomment.textContent=jellyfishstrings.getString('ggcalc_comment0');
    			}
    			else if (result <55 && result>=15) {
    				ggcomment.textContent=jellyfishstrings.getString('ggcalc_comment15');
    			}
    			else if (result <80 && result>=55) {
    				ggcomment.textContent=jellyfishstrings.getString('ggcalc_comment55');
    			}
    			else if (result >=80) {
    				//alert('result >=80');
    				
    				//alert('jellyfishstrings ' + jellyfishstrings);
						ggcomment.textContent =  jellyfishstrings.getString('ggcalc_comment80');
    				//ggcomment.textContent=SS_getStringTableText('ggcalc_comment80');
    				//alert('post comment update');
    			}
    			else {
    				//handle.popup.alert('unhandled result ' + result);
    				ggcomment.textContent=jellyfishstrings.getString('ggcalc_unexpected1');
    			}

  	}
	
}

function t2ppercentinputchange(event) {
	var key = event.keyCode;
	if (key == 13) {
		//enter key
		var result = ggsubmit();
		if (result) {
			window.close();
		}
	}
	var relans = content.document.getElementById('t2prelativeans');
	//var absans = content.document.getElementById('t2pabsans');
	var gginput = content.document.getElementById('t2pinput');
	var percentinput = content.document.getElementById('t2ppercentinput');
	var ggcomment = content.document.getElementById('t2pcomment');
	var jellyfishstrings = window.opener.document.getElementById("swaksmack-strings");

	var svalue=percentinput.value;
	if (svalue==null || svalue=='') {
		ggcomment.textContent='';
		relans.value = "";
		gginput.value = "";
	}
	else if (!numbersOnly(svalue)) {
		percentinput.value=svalue.substring(0,svalue.length-1);
	  ggcomment.textContent=jellyfishstrings.getString('ggcalc_error2');

	}
	else {
		var maindoc = window.opener.content.document;
	
    			var product = content.document.getElementById('t2pproductprice');
    			//convert value to price
    			var dp = null;
    			try {
    				var ftdisc = parseFloat(event.target.value);
    				if (ftdisc>100) {
    					ftdisc = 100;
    				}
    				else if (ftdisc < 0) {
    					ftdisc = 0;
    				}
    				ftdisc = ftdisc/100;
						var dp = swakRetail - (swakRetail*ftdisc);
    				
    				
    			}
    			catch(x) {
    				ggcomment.textContent='Invalid discount value';
    				return;
    			}
    			
    			var result = t2pcalcPercent(swakRetail,dp);

  	}
	
}

function t2pPopulateCurrentPrice() {
	try {
		//var jellyfishstrings = document.getElementById("swaksmack-strings");
		if(window.opener == null || window.opener.closed) {
			var cbox = content.document.getElementById('t2pcomment');
			cbox.value = swakjellyfishstrings.getString('t2pcalc_closedwindow');
			
			t2pStopUpdate();
			return;
		}
		
		
		var currentPrice = content.document.getElementById('t2pproductdisc');
		var cprice =  window.opener.content.document.getElementById('totalCost');
		if (cprice == null) {
			var cbox = content.document.getElementById('t2pcomment');
			cbox.value = swakjellyfishstrings.getString('t2pcalc_noretail');
			t2pStopUpdate();
			return;
		}
		currentPrice.value = cprice.textContent;
	}
	catch(x) {
		t2pStopUpdate();
		return;
	}
}

function t2pcalcPrice(retail, finalprice) {
	//alert('t2pcalcPrice function');
	
	if (retail == null || finalprice == null || finalprice == '') { 
		t2pPopulateCurrentPrice();
		return null;
	}
	try {
		finalprice = parseFloat(finalprice);
	}
	catch(x) {
		t2pPopulateCurrentPrice();
		return null;
	}
	
	
	t2pcalcBase(retail, finalprice,false);

	
}//end calc


function t2pcalcPercent(retail, finalprice) {
	t2pcalcBase(retail, finalprice,true);
}//end calc

function t2pcalcBase(retail, finalprice,isDiscountChange) {
	if (retail == null || finalprice == null) return null;
	try {
	//var jellyfishstrings = document.getElementById("swaksmack-strings");	
	if(window.opener == null || window.opener.closed) {
		var cbox = content.document.getElementById('t2pcomment');
		cbox.value = swakjellyfishstrings.getString('t2pcalc_closedwindow');
		//cbox.value = 'It looks like the JF window is closed.  Seriously, what do you expect me to do now?'
		
		t2pStopUpdate();
		return;
	}
	//alert('finalprice ' + finalprice);
	if (finalprice == '' || finalprice == -1) {
		var cbox = content.document.getElementById('t2pcomment');
		cbox.value = swakjellyfishstrings.getString('t2pcalc_noinput');
		//cbox.value = 'I need something to work with here.';
		t2pPopulateCurrentPrice();
		return null;
	}
	if (isNaN(retail) || isNaN(finalprice)) {
		t2pPopulateCurrentPrice();
		 return null;
	}
	

	
	var relans = content.document.getElementById('t2prelativeans');
//	var absans = content.document.getElementById('t2pabsans');
	var percentinput = content.document.getElementById('t2ppercentinput');
	var priceinput = content.document.getElementById('t2pinput');
	var currentPrice = content.document.getElementById('t2pproductdisc');
	
	var commentbox = content.document.getElementById('t2pcomment');
	
	
	if (finalprice > retail) {
		t2pPopulateCurrentPrice();
		 commentbox.value = swakjellyfishstrings.getString('ggcalc_warn1');
		return "";
	}
	
	var ratio1 = (finalprice/retail);
	var pdiscount = (1-ratio1) *100;

	var currentDiscount = window.opener.swakGetDealDiscount();
	var salePrice = window.opener.swakGetDealPrice(retail,currentDiscount);
	//currentPrice.value = '$' + salePrice;
	var r2 = (1-ratio1) *100;
	var displayCD = Math.round(r2*100)/100;
	if (percentinput != null && isDiscountChange == false) {
		percentinput.value = displayCD;
	}
	if (priceinput != null && isDiscountChange == true) {
		finalprice = Math.round(finalprice*100)/100;
		priceinput.value = finalprice;
	}
	//alert('currentdiscount ' + currentDiscount);
	currentDiscount = currentDiscount/100;
	var relTimeDiscount = (1-currentDiscount-ratio1) *100;
		if (relTimeDiscount <0) {
		if (relans != null) {
			relans.value = "";
		}

		if (commentbox != null) {
			commentbox.value = swakjellyfishstrings.getString('t2pcalc_missed');
			//commentbox.value = "Too Late, price point has passed";
		}
		t2pPopulateCurrentPrice();
		//t2pStopUpdate();
		return;
	}
//	alert('ratio1: ' + ratio1 + ' currentDiscount: ' + currentDiscount+ '  relTimeDiscount: ' + relTimeDiscount);
	
	var secstodiscount = pdiscount/swakSliderRate;
	var relsectodiscount = relTimeDiscount/swakSliderRate;
	
	secstodiscount = Math.round(secstodiscount);
	relsectodiscount = Math.round(relsectodiscount);
	var sec = relsectodiscount;
//	alert('var seconds ' + sec);
	secstodiscount = secstodiscount*1000;
	

	var min = Math.floor(sec/60);
	sec = sec % 60;
	var t = two(sec) + "s";

	var hr = Math.floor(min/60);
	min = min % 60;
	t = two(min) + "m " + t;
	
	hr = hr % 60;
	t = two(hr) + "h " + t;
	
	var dealtime = swakStartTime+ secstodiscount;
	
	var ddate = new Date(dealtime);
	var displaydate = ddate.formatDate("h:i:s A");


	if (relans != null) {
		relans.value = displaydate + '  (' + t + ')';
	}
	/*
	if (absans != null) {
		absans.value = displaydate;
	}
	*/
	if (commentbox != null) {
		commentbox.value = swakjellyfishstrings.getString('t2pcalc_ticktock');
		//commentbox.value = "Tick Tock";
	}
	t2pUpdate();
	t2pPopulateCurrentPrice();
	return 1;
	}
	catch(x) {
		t2pStopUpdate();
	}
}


function two(x) {
	return ((x>9)?"":"0")+x
}

function three(x) {
	return ((x>99)?"":"0")+((x>9)?"":"0")+x
}

function t2pcalcLoad(event) {
	var maindoc = window.opener.content.document;
	initTimetoPriceCalc(maindoc);
	var gginput = content.document.getElementById('t2pinput');
	gginput.focus();
	
	
}
function t2pStopUpdate() {
	if (swakUpdateId != null) {
		clearInterval(swakUpdateId);
	}
}

function t2pUpdate() {
	if (swakUpdateId == null) {
		swakUpdateId = setInterval('t2pAutoUpdate()',3000);
	}
}

function t2pAutoUpdate() {
	//alert('t2pAutoUpdate called');
	try {
		var ti = content.document.getElementById('t2pinput');

		t2pcalcPrice(swakRetail,ti.value);
	
	}
	catch(x) {
		alert(x);
	}
}


function initTimetoPriceCalc(doc)
{ 
			swakjellyfishstrings = document.getElementById("swaksmack-strings");
    	var retaildiv=doc.getElementById('retailPrice');
    	//alert('retaildiv ' + retaildiv.textContent);
    	try {
    		var product = content.document.getElementById('t2pproductprice');
  			product.textContent=retaildiv.textContent; 
  			swakStartTime =  window.opener.swakGetDealStartTime();
  			
  			swakSliderRate = window.opener.swakGetDealSliderDelta();
  			swakRetail = window.opener.swakGetDealRetailPrice();
  	//		alert(swakStartTime + ' rate: ' + swakSliderRate + ' retail: ' + swakRetail);
  	
  	window.addEventListener('unload',  function(event) {
										
										t2pStopUpdate();
											}, false);
  			       t2pcalcPrice(swakRetail,-1);
  			       t2pUpdate();         
     } catch (Exception) {
     		//var swakjellyfishstrings = document.getElementById("swaksmack-strings");
    		var ggerror1 = swakjellyfishstrings.getString('t2pcalc_error1');
    		alert(ggerror1);
   // 		window.close();
     }      
}